package controllers

import (
	"baokaobang/conf"
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"baokaobang/wechat"
	"fmt"
	"os"
	"strings"

	"github.com/go-xorm/xorm"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/sessions"
)

type Expert struct {
	Ctx iris.Context
}

// 专家粉丝列表
func (c *Expert) PostFans() interface{} {
	channel := c.Ctx.PostValueDefault("channel", "mini")

	var userId int64
	if strings.EqualFold(channel, "wechat") {
		token := c.Ctx.GetHeader("token")
		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错：%s", err.Error()), "")
		}
		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%sUserId", channel))
	}

	var err error
	var db *xorm.Engine

	db, err = datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	oneList := make([]models.User, 0)
	err = db.Where("user_type = 1 and is_pay = 1 and parent_id = ?", userId).Find(&oneList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取粉丝列表失败：%s", err.Error()), "")
	}

	twoList := make([]models.User, 0)
	err = db.Where("user_type = 1 and is_pay = 0 and parent_id = ?", userId).Find(&twoList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取粉丝列表失败：%s", err.Error()), "")
	}

	data := map[string]interface{}{
		"oneList": oneList,
		"twoList": twoList,
	}

	return utils.CommonRes(20000, "成功获取粉丝列表", data)
}

// 给粉丝推荐大学
func (c *Expert) PostRecommend() interface{} {
	id := utils.Atoi64(c.Ctx.PostValueDefault("id", "0"))

	if id <= 0 {
		return utils.CommonRes(1000, "请提供推荐大学", "")
	}

	var err error
	var db *xorm.Engine

	db, err = datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	updateData := map[string]interface{}{
		"is_recommend": 1,
	}

	_, err = db.Table(new(models.MatchCollege)).ID(id).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("推荐失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "推荐成功", "")
}

// 获取自己的推广二维码
func (c *Expert) PostQrcode() interface{} {
	channel := c.Ctx.PostValueDefault("channel", "mini")

	session := sessions.Get(c.Ctx)
	userId, _ := session.GetInt64(fmt.Sprintf("%sUserId", channel))

	var err error

	name := fmt.Sprintf("expert_%d.jpg", userId)
	dir := fmt.Sprintf("%s/mini_qrcode", conf.UploadPrefix.Front)
	fileName := fmt.Sprintf("%s/%s", dir, name)
	qrcode := fmt.Sprintf("/mini_qrcode/%s", name)
	_, err = os.Stat(fileName)
	if err != nil {
		qrcode, err = wechat.CreateMiniSceneQrcode(fmt.Sprintf("expert_%d", userId), "pages/login/login", name, 200)
		if err != nil {
			return utils.CommonRes(1000, "推广二维码生成失败：%s", err.Error())
		}
	}

	return utils.CommonRes(20000, "成功获取推广二维码", qrcode)
}

//专家认证接口
func (c *Expert) PostCertification() interface{} {
	channel := c.Ctx.PostValue("channel")
	userType := c.Ctx.PostValue("type")

	if channel == "" || userType == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	var userId int64
	if channel == "wechat" {
		token := c.Ctx.GetHeader("token")
		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错：%s", err.Error()), "")
		}
		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%sUserId", channel))
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	updateData := map[string]interface{}{
		"is_expert": 1,
	}

	if userType == "2" {
		_, err := db.Table(new(models.User)).ID(userId).Update(updateData)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("升级专家出错: %s", err), "")
		}
	}

	return utils.CommonRes(20000, fmt.Sprintf("认证成功"), "")
}
